<?php

function reequipment_userapi_getorders($args)
{
    extract($args);

    /* Fields
    order_id,
	part_number,
	description	,
	model_id,
	equipment_id, 	 	
	company_id, 	 	
	last_ordered, 	 	
	date_expected,
	man_part_number,	 
	cost,
	agent,
	tracking_number,
	return_auth,	 	 
	credit_memo,	 
	date_returned
	*/

    // Optional arguments
    if (!isset($startnum)) {$startnum = 1;}
    if (!isset($numitems)) {$numitems = -1;}
    
   if (isset($over_due)){
		$over_due = time();
	}

    $dbconn =& xarDBGetConn();
    $xartable =& xarDBGetTables();
    $table = $xartable['reordered'];
    $join_equipment = $xartable['reequipment'];
    $join_model = $xartable['remodels'];
    $join_area = $xartable['reareas'];
    $join_company = $xartable['rel_eq'];

    $query = "SELECT DISTINCT o.order_id,
					o.part_number,
					o.description,
					o.model_id,
					o.equipment_id,                                                                                                       
					o.company_id,                                                                                                         
					o.last_ordered,                   
					o.man_part_number,
					o.cost,
					o.date_expected,
					o.agent,
					o.tracking_number,
					o.return_auth,
					o.credit_memo,
					o.date_returned,
					e.serial_number,
					e.cycles,
					e.machine_name,
					m.model_name,
					a.area_name,
					r.company_name"
    		. " FROM $table o"
    		
    		. " LEFT OUTER JOIN $join_model m"
    		. " ON o.model_id = m.model_id"
    		
    		. " LEFT OUTER JOIN $join_area a"
    		. " ON m.area_id = a.area_id"
    		
    		. " LEFT OUTER JOIN $join_equipment e"
    		. " ON o.equipment_id = e.equipment_id"    	
    		
    		. " LEFT OUTER JOIN $join_company r"
    		. " ON o.company_id = r.company_id";       		

    $where = array();
    $bind = array();
    
    if (!empty($order_id)) {
        $where[] = 'o.order_id = ?';
        $bind[] = (int)$order_id;
    }
    
    if (!empty($part_number)) {
        $where[] = 'o.part_number = ?';
        $bind[] = (string)$part_number;
    }    
    
    if (!empty($model_id)) {
        $where[] = 'o.model_id = ?';
        $bind[] = (int)$model_id;
    }    

    if (!empty($equipment_id)) {
        $where[] = 'o.equipment_id = ?';
        $bind[] = (int)$equipment_id;
    }  
    
    if (!empty($company_id)) {
        $where[] = 'o.company_id = ?';
        $bind[] = (int)$company_id;
    }      
    
    // Figure out the dates when we need it.
    if (!empty($over_due)) {
        $where[] = 'o.date_expected < ?';
        $bind[] = (int)$over_due;
    }   

    if (!empty($man_part_number)) {
        $where[] = 'o.man_part_number = ?';
        $bind[] = (string)$man_part_number;
    }    

    if (!empty($where)) {
        $query .= ' WHERE ' . implode(' AND ', $where);
    }

    // Not really necessary
    $query .= " ORDER BY o.last_ordered DESC";

    $result =& $dbconn->SelectLimit($query, $numitems, $startnum-1, $bind);
    if (!$result) return;

    $tags = array();
    for (; !$result->EOF; $result->MoveNext()) {
        list($order_id,
				$part_number,
				$description,
				$model_id,
				$equipment_id,                                                                                                       
				$company_id,                                                                                                         
				$last_ordered,                   
				$man_part_number,
				$cost,
				$date_expected,
				$agent,
				$tracking_number,
				$return_auth,
				$credit_memo,
				$date_returned,
				$serial_number,
				$cycles,
				$machine_name,
				$model_name,
				$area_name,
				$company_name) = $result->fields;

        $tags[] = array(
        		'order_id'				=> $order_id,
        		'part_number'		=> xarVarPrepForDisplay($part_number),
        		'description'			=> xarVarPrepForDisplay($description),
                'model_id'      		=> $model_id,
                'equipment_id'      	=> $equipment_id,
                'company_id'			=> $company_id,
                'last_ordered' 		=> xarLocaleGetFormattedDate('long', $last_ordered),
                'man_part_number'=> xarVarPrepForDisplay($man_part_number),
                'cost' 					=> round($cost, 2),
                'date_expected' 		=> xarLocaleGetFormattedDate('long', $date_expected),
                'agent'    				=> xarVarPrepForDisplay($agent),
                'tracking_number'  => xarVarPrepForDisplay($tracking_number),
                'return_auth'    		=> xarVarPrepForDisplay($return_auth),
                'credit_memo'    	=> xarVarPrepForDisplay($credit_memo),
                'date_returned' 		=> xarLocaleGetFormattedDate('long', $date_returned),
                'serial_number'    	=> xarVarPrepForDisplay($serial_number),
                'cycles'					=> $cycles,
                'machine_name'    	=> xarVarPrepForDisplay($machine_name),
                'model_name'    	=> xarVarPrepForDisplay($model_name),
                'area_name'    		=> xarVarPrepForDisplay($area_name), 
                'company_name'    => xarVarPrepForDisplay($company_name)              
        );
    }

    $result->Close();
    return $tags;
}
?>